﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameJamSnake
{
    class Tile
    {
        Point m_reference;

        public enum TileState { EMPTY, DEAD, STONE, BONUS, POWER, TIME, WALL }
        private TileState m_tileState;

        Texture2D m_stoneTexture;
        Texture2D m_deadSnakeTexture;
        Texture2D m_powerTexture;
        Texture2D m_timeTexture;

        public const int CELL_WIDTH = 40;
        public const int CELL_HEIGHT = 40;

        public int multiplyer = 1;

        public Tile(int i, int j)
        {
            m_reference.X = i;
            m_reference.Y = j;
            m_tileState = TileState.EMPTY;
        }

        public void Load(Texture2D deadSnake, Texture2D stone, Texture2D power, Texture2D clock)
        {
            m_deadSnakeTexture = deadSnake;
            m_stoneTexture = stone;
            m_powerTexture = power;
            m_timeTexture = clock;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle srcRect = new Rectangle(0, 0, 40, 40);
            Rectangle srcRectTime = new Rectangle(0, 0, 35, 38);
            Rectangle srcRectStone = new Rectangle(0, 0, 80, 80);
            Rectangle destRect = new Rectangle(m_reference.X * Tile.CELL_WIDTH, m_reference.Y * Tile.CELL_HEIGHT, Tile.CELL_WIDTH * multiplyer, Tile.CELL_HEIGHT * multiplyer);

            switch (m_tileState)
            {
                case TileState.DEAD:
                    spriteBatch.Draw(m_deadSnakeTexture, destRect, srcRect, Color.Gray);
                    break;
                case TileState.STONE:
                    spriteBatch.Draw(m_stoneTexture, destRect, srcRectStone, Color.White);
                    break;
                case TileState.BONUS:
                    spriteBatch.Draw(m_stoneTexture, destRect, srcRect, Color.Brown);
                    break;
                case TileState.POWER:
                    spriteBatch.Draw(m_powerTexture, destRect, srcRect, Color.White);
                    break;
                case TileState.TIME:
                     spriteBatch.Draw(m_timeTexture, destRect, srcRectTime, Color.White);
                    break;
                case TileState.WALL:
                    spriteBatch.Draw(m_deadSnakeTexture, destRect, srcRect, Color.White);
                    break;
            }
        }
        public int GetMultiplyer()
        {
            return multiplyer;
        }

        public TileState GetState()
        {
            return m_tileState;
        }

        public Rectangle GetRectangle()
        {
            Rectangle rect = new Rectangle(m_reference.X * Tile.CELL_WIDTH, m_reference.Y * Tile.CELL_HEIGHT, Tile.CELL_WIDTH * multiplyer, Tile.CELL_HEIGHT * multiplyer);
            return rect;
        }

        public void Die()
        {
            m_tileState = TileState.DEAD;
        }

        public void TurnWall()
        {
            m_tileState = TileState.WALL;
        }
        public void EmptyTile()
        {
            m_tileState = TileState.EMPTY;
        }

        public void PowerUp()
        {
            m_tileState = TileState.POWER;
        }
        public void TimeUp()
        {
            m_tileState = TileState.TIME;
        }

        public void Petrify()
        {
            m_tileState = TileState.STONE;
            multiplyer *= 2;
        }

        public void SetBonus()
        {
            m_tileState = TileState.BONUS;
        }

        public void Multiply()
        {
            multiplyer *= 2;
        }

        public void SetMultiplyer(int i)
        {
            multiplyer = i;
        }

        //public bool IsDead()
        //{
        //    if (m_tileState == TileState.DEAD)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
    }
}
